package one.stand.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import java.time.Duration;

/**
 * @author: QIK
 * @CreateDate: 2019/4/10 16:44
 */
@EnableCaching
@Configuration
public class CacheManagerConfig {
    private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        logger.info("生成RedisCacheManager缓存管理器...");
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(60))
                .disableCachingNullValues();

        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();

        return cacheManager;
    }
}
